<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<title>用户密码修改</title>
		<link href="../../component/pear/css/pear.css" rel="stylesheet">
	</head>
	<body class="pear-container">
		<form class="layui-form" lay-filter="form_password" id="form_password">
			<div class="layui-row layui-col-space10">
				<div class="layui-col-md12">
					<div class="layui-card" lay-filter="component-form-element">
						<div class="layui-card-header">密码修改</div>
						<div class="layui-card-body layui-row layui-col-space10">
							<div class="layui-col-md12">
								<div class="layui-form-item">
									<div class="layui-input-wrap layui-inline">
									  <div class="layui-input-prefix">
										<i class="layui-icon layui-icon-password"></i>
									  </div>
									  <input type="password" name="oldPassword" value="" lay-verify="required" placeholder="旧密码" autocomplete="off" class="layui-input" lay-affix="eye">
									</div>
								</div>
								<div class="layui-form-item">
									<div class="layui-input-wrap layui-inline">
									  <div class="layui-input-prefix">
										<i class="layui-icon layui-icon-password"></i>
									  </div>
									  <input type="password" name="newPassword" value="" lay-verify="required" placeholder="新密码" autocomplete="off" class="layui-input" id="newPassword" lay-affix="eye">
									</div>
								</div>
								<div class="layui-form-item">
									<div class="layui-input-wrap layui-inline">
										<div class="layui-input-prefix">
										<i class="layui-icon layui-icon-password"></i>
										</div>
										<input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword" placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye">
									</div>
								</div>
								<div class="layui-input-block">
									<button type="submit" class="pear-btn pear-btn-primary" lay-submit lay-filter="password_save">保存</button>
									<button type="reset" class="pear-btn">重置</button>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</form>
		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
			layui.use(['form','util','jquery','popup'], function(){
				var $ = layui.$;
				var form = layui.form;
				var layer = layui.layer;
				var util = layui.util;
				var popup = layui.popup;

				// 自定义验证规则
				form.verify({
					// 确认密码
					confirmPassword: function(value, item){
						var passwordValue = $('#newPassword').val();
						if(value !== passwordValue){
							return '两次密码输入不一致';
						}
					}
				});

				// 提交事件
				form.on('submit(password_save)', function(data) {
					var field = data.field; // 获取表单字段值

					$.post('/user/password/update',  field, function(data) {
						if (data && data.code == 200) {
							popup.success("已修改");
							$("#form_password").trigger("reset");
							return true;
						} else {
							popup.failure(data.msg);
							return false;
						}
					}, "json");

					return false; // 阻止默认 form 跳转
				});
			});
		</script>
	</body>
</html>
